use Tk;
use strict;
use encoding 'euc_cn';
require Tk::NoteBook;
use Time::Local;
use POSIX qw(strftime);

my $filedir = "E:\\notebook";
mkdir $filedir unless -e $filedir;

my $ymd = qx(date /T);
$ymd =~ /^(\d+)\/(\d+)\/(\d+)/;
$ymd = $1 . $2 . $3;
my $filename1 = "notebook_" . $ymd . ".txt";
my $ysd       = strftime "%Y%m%d", localtime( time - 86400 );
my $filename2 = "notebook_" . $ysd . ".txt";
my $mw        = MainWindow->new;
$mw->geometry("500x500");
$mw->configure( -title => '我的记事本' );

my $nb = $mw->NoteBook()->pack( -side => 'right', -fill => 'both' );

my $p1 = $nb->add( 'page1', -label => '今日点滴' );
my $p2 = $nb->add( 'page2', -label => '回顾往昔' );

my $t1 =
$p1->Scrolled("Text")
->pack( -side => 'bottom', -fill => 'both', -expand => 1 );
my $t2 =
$p2->Scrolled("Text")
->pack( -side => 'bottom', -fill => 'both', -expand => 1 );

$t1->delete( "1.0", "end" );
open( CURF, "$filedir\\$filename1" );
while (<CURF>) { $t1->insert( "end", $_ ); }
close CURF;

$t2->delete( "1.0", "end" );
open( YEDF, "$filedir\\$filename2" );
while (<YEDF>) { $t2->insert( "end", $_ ); }
close YEDF;

my $b_s1 = $p1->Button(
		-text             => '保存',
		-background       => 'green',
		-foreground       => 'white',
		-activeforeground => 'yellow',
		-command          => \&save_file1
		)->pack( -side => 'right', -expand => 1 );
my $l1 = $p1->Label(
		-text       => '退出前请保存！！',
		-font       => '楷体',
		-foreground => 'red'
		)->pack( -side => 'right', -expand => 1 );

my $l2 =
$p2->Label( -text => '请输入日期：', -font => '宋体' )
->pack( -side => 'left', -expand => 1 );
my $e1 = $p2->Entry(
		-background   => 'white',
		-foreground   => 'black',
		-textvariable => \$ysd
		)->pack( -side => 'left', -expand => 1, -after => $l2 );

my $b_l1 = $p2->Button(
		-text             => '查询',
		-background       => 'green',
		-foreground       => 'white',
		-activeforeground => 'yellow',
		-command          => \&load_file1
		)->pack( -side => 'left', -expand => 1 );
my $b_s2 = $p2->Button(
		-text             => '保存',
		-background       => 'blue',
		-foreground       => 'red',
		-activeforeground => 'yellow',
		-command          => \&save_file2
		)->pack( -side => 'right', -expand => 1 );
my $l3 = $p2->Label(
		-text       => '退出前请保存:',
		-font       => '楷体',
		-foreground => 'red'
		)->pack( -side => 'right', -expand => 1 );

MainLoop;

sub save_file1 {
	open( SF, ">$filedir\\$filename1" );
	print SF $t1->get( "1.0", "end" );
	close SF;
}

sub load_file1 {
	my $lastdate = $e1->get();
	my $filename = "notebook_" . $lastdate . ".txt";
	$t2->delete( "1.0", "end" );
	open( LASTF, "$filedir\\$filename" );
	while (<LASTF>) {
		$t2->insert( "end", $_ );
	}
	close CURF;
}

sub save_file2 {
	my $lastdate = $e1->get();
	my $filename = "notebook_" . $lastdate . ".txt";
	open( SF, ">$filedir\\$filename" );
	print SF $t2->get( "1.0", "end" );
	close SF;
}